/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2022-10-31
 * Time: 21:54
 */
import java.util.Scanner;

public class Test {
    //在同一个类中定义多个方法：要求不仅可以求2个整数的最大值，还可以求3个小数的最大值？
    public static int MyMax(int a, int b){
        if (a > b)
            return a;
        else
            return b;
    }
    public static double MyMax(double a, double b){
        if (a > b)
            return a;
        else
            return b;
    }
    public static double MyMax(double a, double b, double c){
        return MyMax(MyMax(a, b), c);
    }

    public static void main(String[] args) {
        System.out.println(MyMax(1,2));
        System.out.println(MyMax(1.1,2.2,3.3));

    }






    //在同一个类中,分别定义求两个整数的方法 和 三个小数之和的方法。 并执行代码，求出结果
    public static int sum(int a, int b){
        return a +b;
    }
    public static double sum(double a, double b, double c){
        return a + b + c;
    }

    public static void main15(String[] args) {
        System.out.println(sum(1,2));
        System.out.println(sum(1.1,2.2,3.3));


    }



    //求斐波那契数列的第n项。(迭代实现)
    public static void main14(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()){
            int num = scanner.nextInt();
            int a = 1;
            int b = 1;
            while (num > 2){
                int temp = b;
                b = a + b;
                a = temp;
                num--;
            }
            System.out.println(b);
        }
    }








    //创建方法求两个数的最大值max2，随后再写一个求3个数的最大值的函数max3。要求：在max3这个函数中，调用max2函数，来实现3个数的最大值计算
    public static int max2(int a, int b){
        if (a > b)
            return a;
        else
            return b;
    }
    public static int max3(int a, int b, int c){
        return max2(max2(a,b), c);
    }
    public static void main13(String[] args) {
        System.out.println(max2(5, 6));
        System.out.println(max3(7,8,9));
    }



    //输出n*n的乘法口诀表，n由用户输入。
    public static void main12(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()){
            int num = scanner.nextInt();
            for (int i = 1; i <= num; i++) {
                for (int j = 1; j <= i; j++) {
                    System.out.printf("%d*%d=%d\t", i, j, i * j);
                }
                System.out.println();
            }
        }
    }
    
    
    
    
    //编写代码模拟三次密码输入的场景。 最多能输入三次密码，密码正确，提示“登录成功”,密码错误， 可以重新输 入，最多输入三次。三次均错，则提示退出程序
    public static void main11(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int password = 123;
        int count = 0;
        while (count != 3){
            int ans = scanner.nextInt();
            if (password == ans) {
                System.out.println("登陆成功");
                break;
            }else if (count == 2){
                System.out.println("密码错误，退出");
            }else {
                System.out.println("密码错误，请重试");
            }
            count++;
        }
    }

    //输出一个整数的每一位，如：123的每一位是3，2，1
    public static void main10(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()){
            int num = scanner.nextInt();
            while (num != 0){
                System.out.println(num % 10);
                num /= 10;
            }
        }
    }

    //计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值 。
    public static void main9(String[] args) {
        double sum = 0;
        int k = 1;
        for (int i = 1; i <= 100; i++) {
            sum += 1.0 / i * k;
            k *= -1;
        }
        System.out.println(sum);
    }


    public static void main8(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            for (int i = 0; i < a; i++) {
                for (int j = 0; j < a; j++) {
                    if (i == j || (i + j) == a - 1)
                        System.out.print('*');
                    else
                        System.out.print(' ');
                }
                System.out.println();
            }
        }
    }





    //给定两个数，求这两个数的最大公约数
    public static void main7(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()){
            int num1 = scanner.nextInt();
            int num2 = scanner.nextInt();
            int temp = num1 % num2;
            while (temp != 0){
                num1 = num2;
                num2 = temp;
                temp = num1 % num2;
            }
            System.out.println("最大公约数为：" + num2);
        }
    }



    //求一个整数，在内存当中存储时，二进制1的个数
    public static void main6(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int count = 0;
        //大神解法
        while (n != 0) {
            n = n & (n-1) ;
            count++;
        }

        System.out.println(count);
    }

    public static void main5(String[] args) {
        Scanner scan = new Scanner(System.in);
        while (scan.hasNextInt()){
            int num = scan.nextInt();
            int count = 0;
            for (int i = 0; i < 32; i++) {
                if (((num >>> i) & 1) != 0)
                    count++;
            }
            System.out.println(count);
        }
    }


    //给定一个数字，判定一个数字是否是素数
    public static void main4(String[] args) {
        Scanner scan = new Scanner(System.in);
        while (scan.hasNextInt()) {
            int num = scan.nextInt();
            int i = 0;
            for (i = 2; i <= Math.sqrt(num); i++) {
                if (num % i == 0)
                    break;
            }
            if (i < Math.sqrt(num))
                System.out.println(num + "不是素数");
            else
                System.out.println(num + "是素数");
        }
    }


    //输出 1000 - 2000 之间所有的闰年
    public static void main3(String[] args) {
        for (int i = 1000; i <= 2000; i++) {
            if (i % 400 == 0 || i % 4 == 0 && i / 100 != 0)
                System.out.println(i);
        }
    }



    //编写程序数一下 1到 100 的所有整数中出现多少个数字9
    public static void main2(String[] args) {
        int count = 0;

        for (int i = 1; i <= 100; i++) {
            if (i % 10 == 9)
                count++;
            if (i / 10 == 9)
                count++;
        }

        System.out.println("数字9的个数：" + count);
    }


    //100以内的素数
    public static void main1(String[] args) {
        for (int i = 2; i <= 100; i++) {
            int j;
            for ( j = 2; j <= Math.sqrt(i); j++) {
                if (i % j == 0)
                    break;
            }
            if (j > Math.sqrt(i))
                System.out.println(i);
        }
    }

}
